home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-29 | 3.4 KB | 154 lines | [TEXT/Rich] |
- { graphical simulation class }
- { written by Tim Budd, Oregon State University }
- { April 1990 }
-
- unit graphicUniverse;
-
- interface
-
- type
-
- GraphicalObject = object
- link: GraphicalObject;
- region: Rect;
- procedure moveTo (x, y: integer);
- function intersectsWith (anObject: GraphicalObject): boolean;
- procedure erase;
- { the following overridden in subclasses }
- procedure update;
- procedure draw;
- procedure hitBy (anObject: GraphicalObject);
- end;
-
- ObjectUniverse = object
- moveableObjects: GraphicalObject;
- fixedObjects: GraphicalObject;
- continueUpdate: boolean;
- procedure initialize;
- procedure installFixedObject (newObj: GraphicalObject);
- procedure installMovableObject (newObj: GraphicalObject);
- procedure draw;
- procedure updateMoveableObjects;
- procedure continueSimulation;
- function hitObject (anObject: GraphicalObject):GraphicalObject;
- end;
-
- implementation
-
- procedure GraphicalObject.moveTo (x, y: integer);
- begin
- OffsetRect(region, region.top, region.left);
- OffsetRect(region, x, y);
- end;
-
- procedure GraphicalObject.update;
- begin
- { implemented in subclass }
- end;
-
- procedure GraphicalObject.erase;
- begin
- EraseRect(region);
- end;
-
- procedure GraphicalObject.draw;
- begin
- { implemented in subclass }
- end;
-
- function GraphicalObject.intersectsWith (anObject: GraphicalObject):
- boolean;
- var
- theIntersection: Rect;
- begin
- intersectsWith := SectRect(region, anObject.region, theIntersection);
- end;
-
- procedure GraphicalObject.hitBy (anObject: GraphicalObject);
- begin
- { behavior provided by subclass }
- end;
-
- procedure ObjectUniverse.initialize;
- begin
- fixedObjects := nil;
- moveableObjects := nil;
- end;
-
- procedure ObjectUniverse.installFixedObject (newObj: GraphicalObject);
- begin
- newObj.link := fixedObjects;
- fixedObjects := newObj;
- end;
-
- procedure ObjectUniverse.installMovableObject;
- begin
- newObj.link := moveableObjects;
- moveableObjects := newObj;
- end;
-
- procedure ObjectUniverse.updateMoveableObjects;
- var
- currentObject: GraphicalObject;
- begin
- repeat
- continueUpdate := false;
- currentObject := moveableObjects;
- while currentObject <> nil do
- begin
- currentObject.update;
- currentObject := currentObject.link;
- end;
- until not continueUpdate
- end;
-
- procedure ObjectUniverse.continueSimulation;
- begin
- continueUpdate := true
- end;
-
- procedure ObjectUniverse.draw;
- var
- currentObject: GraphicalObject;
- begin
- currentObject := fixedObjects;
- while currentObject <> nil do
- begin
- currentObject.draw;
- currentObject := currentObject.link;
- end;
- currentObject := moveableObjects;
- while currentObject <> nil do
- begin
- currentObject.draw;
- currentObject := currentObject.link;
- end;
- end;
-
- function ObjectUniverse.hitObject (anObject: GraphicalObject):
- GraphicalObject;
- var
- currentObject: GraphicalObject;
- hit: GraphicalObject;
- begin
- currentObject := fixedObjects;
- hit := nil;
- while (hit = nil) and (currentObject <> nil) do
- begin
- if (anObject <> currentObject) then
- if (anObject.intersectsWith(currentObject)) then
- hit := currentObject;
- currentObject := currentObject.link;
- end;
- currentObject := moveableObjects;
- while (hit = nil) and (currentObject <> nil) do
- begin
- if (anObject <> currentObject) then
- if (anObject.intersectsWith(currentObject)) then
- hit := currentObject;
- currentObject := currentObject.link;
- end;
- hitObject := hit;
- end;
- end.
-